<?php
    extract(extractPost(), EXTR_PREFIX_ALL, 'd');
    if(isset($d_board_id) && isset($d_project_id)){
        
        //R::trash(R::load('board', 9));
        $board = R::load('board', $d_board_id);
        if($board->id == 0){
            jsonErrorDie("Board with provided id does not exist.");
        }
        $project = R::load('project',$d_project_id);
        if($project->id == 0){
            jsonErrorDie("Project with provided id does not exist.");
        }
        
        if($project->board_id != NULL){
            jsonErrorDie("Table for this project already exists.");
        }
        
        $dupBoard = R::dup($board, false, false, false);
        //$dupBoard = R::dispense( 'board' );
        //$dupBoard->name = getNextCopyName($board->name);
        //$dupBoard->group = $board->group;
        //$dupBoard->date_created = getCurrentDate();
        //$dupBoard->sharedBoardColumns = array();
        //$dupBoard->wip = $board->wip;

        
        $dupBoard->ownProjectList = array( $project );
        //$dupBoard = R::dup($board, true, false, array('board','column'));
        
        // Clear all cards from columns
        foreach ($board->ownColumnList as $col) {
            $colDup = R::dup($col, false, false, false);
            $dupBoard->ownColumnList[] = $colDup;
        }
        $dupBoard->name = $project->name; //getNextCopyName($board->name);
        R::store($dupBoard);
        
        
        $boards = R::getAll( 'SELECT * FROM board WHERE id = ?', array($dupBoard->id) );
        
        foreach ($boards as &$board){

            $board["projects"] = R::getAll( 'SELECT id, code, name FROM project WHERE board_id = ? AND active = 1', array($board['id']));
        
            foreach ($board["projects"] as &$project){

                $baseColumns = R::getAll( 'SELECT DISTINCT name FROM `column` WHERE `board_id` = ? ORDER BY position ASC', array($board['id'])); 
                $columns = array();
                foreach ($baseColumns as $colName){
                    $column = array();
                    $colName = $colName["name"];
                    $subcolumns = R::getAll( 'SELECT * FROM `column` WHERE `board_id` = ? AND `name` = ? ORDER BY position ASC', array($board['id'], $colName));

                    if(count($subcolumns) == 1 && is_null($subcolumns[0]["subcolumn"])){
                        $column = $subcolumns[0];
                        unset($column["subcolumn"]);
                        $column["cards"] = getCardsForColumn($column["id"]);
                    }
                    else{
                        $column["name"] = $colName;
                        foreach($subcolumns as &$subcol){
                            $subcol["name"] = $subcol["subcolumn"];
                            unset($subcol["subcolumn"]);
                            $subcol["cards"] = getCardsForColumn($subcol["id"]);

                        }
                        $column["subcolumn"] = $subcolumns;
                    }
                    $columns[] = $column;

                }
                $project["columns"] = $columns;
            }
        }
        
        jsonSuccessDie("allBoards", $boards);
    }
    else{
        jsonErrorDie("Not all mandatory data provided.");
    }
    
    
    
    
    
    function getNextCopyName($startName){
        $i = 1;
        $startName .= " copy ";
        while (true) {
            if(R::count('board',' name = ?',array(($startName . $i))) == 0){
                return $startName . $i;
            }
            $i = $i + 1;
        }
    }

    // Copy of function can be found in getBoards.php
    function getCardsForColumn($colId){
        $cards = R::getAll( 'SELECT * FROM card WHERE column_id = ?' , array($colId));
        foreach($cards as &$card){
            $cardDate = $card["due_date"]; 
            if(isLessThan3DaysRemaining($cardDate)){
                $card["expiring"] = true;
            }
            else{
                $card["expiring"] = false;
            }
            //TODO: $cards["log"] = //NEKI:
        }
        return $cards;
    }